<template>
  <div>
    <el-dialog
      width="1024px"
      :close-on-click-modal="false"
      :show-close="true"
      v-bind="$attrs"
      v-on="$listeners"
      @open="onOpen"
      @close="onClose"
    >
      <div slot="title">
        {{ formData.id != null ? '修改线索' : '新增线索' }}
      </div>
      <div class="sketch_content">
        <el-form label-position="top" ref="elForm" :model="formData" :rules="rules" label-width="80px">
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="线索名称" prop="leadsName">
                <el-input v-model="formData.leadsName" style="width: 100%"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="线索来源" prop="sourceId">
                <el-select v-model="formData.sourceId" placeholder="请选择" style="width: 100%">
                  <el-option
                    v-for="dict in dict.type.crm_source_name"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="手机" prop="mobile">
                <el-input v-model="formData.mobile" prefix-icon="el-icon-mobile-phone" style="width: 100%"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="电话" prop="telephone">
                <el-input v-model="formData.telephone" prefix-icon="el-icon-phone-outline" style="width: 100%"/>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="邮箱" prop="email">
                <el-input v-model="formData.email" prefix-icon="el-icon-message" style="width: 100%"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="地址" prop="address">
                <el-input v-model="formData.address" prefix-icon="el-icon-discover" style="width: 100%"/>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="客户行业" prop="industryId">
                <el-select v-model="formData.industryId" placeholder="请选择" style="width: 100%">
                  <el-option
                    v-for="dict in dict.type.crm_customer_industry"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="客户级别" prop="levelId">
                <el-select v-model="formData.levelId" placeholder="请选择" style="width: 100%">
                  <el-option
                    v-for="dict in dict.type.crm_customer_level"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="下次联系时间" prop="nextTime">
                <el-date-picker value-format="yyyy-MM-dd HH:mm:ss" v-model="formData.nextTime" type="datetime"
                                placeholder="请选择" style="width: 100%"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="24">
              <el-form-item label="备注" prop="remark">
                <el-input
                  type="textarea"
                  :autosize="{ minRows: 3, maxRows: 5}"
                  placeholder="请输入内容"
                  v-model="formData.remark"
                >
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" type="primary" @click="submitForm" :loading="submitLoading">保 存</el-button>
        <el-button size="small" @click="onClose">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  saveHasKey,
  selectByPrimaryKey,
  updateByPrimaryKeySelective
} from '@/api/crm/Leads'

export default {
  inheritAttrs: false,
  name: 'leadsDialog',
  components: {},
  dicts: ['crm_source_name', 'crm_customer_industry', 'crm_customer_level'],
  props: {
    pk: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      submitLoading: false, // 提交按钮加载动画
      formData: {
        id: null,// 主键id
        leadsName: '',// 线索名称
        email: null,// 邮箱
        sourceName: null,// 线索来源
        sourceId: undefined,// 线索来源id
        mobile: null,// 手机
        telephone: null,// 电话
        address: null,// 地址
        industryName: null,// 客户行业
        industryId: undefined,// 客户行业id
        levelName: null,// 客户级别
        levelId: undefined,// 客户级别id
        nextTime: null,// 下次联系时间
        remark: null// 备注
      },
      // 规则
      rules: {
        leadsName: [{ required: true, message: '线索名称不能为空', trigger: 'blur' }]
      }
    }
  },
  // 在实例创建完成后被立即调用
  created() {
  },
  // 实例被挂在后调用
  mounted() {

  },
  // 方法
  methods: {
    // 对话框打开
    onOpen() {
      if (this.pk != null) {
        selectByPrimaryKey(this.pk).then(res => {
          this.formData = res.data
        })
      }
    },
    // 对话框关闭
    onClose() {
      this.submitLoading = false
      this.$refs['elForm'].resetFields()
      this.$emit('update:visible', false)
    },
    // 提交
    submitForm() {
      this.submitLoading = true
      this.$refs['elForm'].validate(valid => {
        if (valid) {
          if (this.formData.id != undefined) {
            // 修改
            updateByPrimaryKeySelective(this.formData).then(res => {
              this.$modal.msgSuccess('修改成功')
              this.$emit('ok')
              this.onClose()
            })
          } else {
            // 新增
            saveHasKey(this.formData).then(res => {
              this.$modal.msgSuccess('添加成功')
              this.$emit('ok')
              this.onClose()
            })
          }
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
